/*
 *   Copyright (C) 2007 by Andrzej Oczkowicz
 *   andrew_lz@poczta.fm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *   KMediaCatalog :)
 *
 */
#ifndef KMODMGR_H
#define KMODMGR_H

#include <QObject>
#include <QLibrary>
#include <QFileInfo>

class kModule;
class kInputBasic;
class kInputArchive;
class kOutputBasic;

struct kBasicFileInfo;
struct kInfoResult;

/**
	@author Andrzej Oczkowicz <andrew_lz@poczta.fm>
*/

typedef quint16 kMagic;
typedef kModule *(*kModuleInitProc)(void);
typedef void (*kModuleExitProc)(void);
typedef struct kModuleEntry{
  QLibrary * lib;
  kModuleInitProc init;
  kModuleExitProc exit;
  kModule *object;
  kMagic magic;
};

typedef QList<kModuleEntry*> kModulesInfo;

typedef QList<kInputBasic*>   kInBasicList;
typedef QList<kInputArchive*> kInArchList;
typedef QList<kOutputBasic*>  kOutList;

typedef struct kPluginConfig{
  bool active;
  kModule * owner;
};

typedef struct kInputBasicInfo{
  kPluginConfig cfg;
  kInputBasic *plugin;
};

typedef struct kInputArchiveInfo{
  kPluginConfig cfg;
  kInputArchive *plugin;
};

typedef struct kOutputPluginInfo{
  kPluginConfig cfg;
  kOutputBasic *plugin;
};

typedef QList<kInputBasicInfo>   kListInBasicInfo;
typedef QList<kInputArchiveInfo> kListInArchInfo;
typedef QList<kOutputPluginInfo> kListOutInfo;



class kModMgr : public QObject
{
Q_OBJECT
public:
    kModMgr(QObject *parent = 0);
    ~kModMgr();

private:
    kModulesInfo    modules;
    kModuleEntry  * main;

    kListInBasicInfo listInBasic;
    kListInArchInfo  listInArch;
    kListOutInfo     listOut;

    struct kNode * rootNode;

private:
    void freeAllModules();
    void modTryAdd(kModuleEntry *e);
    bool modLoad(kModuleEntry *e);
    void modUnLoad(kModuleEntry *e);
    void modFree(kModuleEntry *e);
    kModuleEntry * moduleGetEntry(const QString&mod);
    kMagic modMagic(const QString&mod);
    int findAllModules(const QString&dir);
    bool loadMainModule();
    void findModules();
    void resolvePlugins();

    void importedInfoToBasicInfo(QFileInfo*fi, kInfoResult*res, kBasicFileInfo & basic);
    void clearResStruct(kInfoResult&res);
public:
    int startScanning();
    int stopScanning();
    int processFile(QFileInfo * fi);

friend class kMaster;

};

#endif
